package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwefT;
import cn.e4j.modular.crm.mapper.Gweq024Mapper;
import cn.e4j.modular.crm.service.Gweq024Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Service
public class Gweq024ServiceImpl extends ServiceImpl<Gweq024Mapper, GwefT> implements Gweq024Service {
    @Override
    public List<Dto> getList(Page<Object> page, Dto inDto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        List<Dto> resultDto= Lists.newArrayList();

        String USER_ID = shiroUser.getID();
        String ENT = shiroUser.getDBC();
        String LANG = LangUtils.getLang(shiroUser.getLANG());

        inDto.put("ENT", ENT);
        inDto.put("USER_ID",USER_ID);
        inDto.put("LANG",LANG);

        // 获取可用营运中心列表
        /*List<Dto> sysOrg_= this.baseMapper.getBizCodeList();
        if(sysOrg_.size() == 0){
            return new ArrayList<>();
        }

        List<String> plist = Lists.newArrayList();
        for (Dto org : sysOrg_) {
            plist.add(org.getString("BIZ_CODE"));
        }
        inDto.put("GWEFPLANT",plist);*/

        // 查询列表数据
//        List<Dto> gwefs = this.baseMapper.listPage(page,inDto);
        List<Dto> gwefs = InvokeUtil.invokeBeanMethod("gweq024Mapper,listPage",page,inDto);

        Date date = new Date();
        for (Dto gwef : gwefs) {
            int cnt=0;
            if(inDto.getString("STANDARD").equals("1")){
                Date seconddate = DateUtils.parseDate(DateUtils.formatDate(date,"yyyy-MM-dd"));
                Date firstdate = DateUtils.parseDate(DateUtils.formatDate(gwef.getDate("GWEC002"),"yyyy-MM-dd"));
                cnt = this.longOfTwoDate(firstdate, seconddate);
            }else if(inDto.getString("STANDARD").equals("2")){
                Date seconddate = DateUtils.parseDate(DateUtils.formatDate(date,"yyyy-MM-dd"));
                Date firstdate = DateUtils.parseDate(DateUtils.formatDate(gwef.getDate("GWEFDOCDATE"),"yyyy-MM-dd"));
                cnt = this.longOfTwoDate(firstdate, seconddate);
            }

            BigDecimal gwef011 = gwef.getBigDecimal("GWEF011");

            if(cnt>=0 && cnt <=30){
                gwef.put("DAY0_30",gwef011);
            }else if(cnt>=31 && cnt <=60){
                gwef.put("DAY31_60",gwef011);
            }else if(cnt>=61 && cnt <=90){
                gwef.put("DAY61_90",gwef011);
            }else if(cnt>=91 && cnt <=180){
                gwef.put("DAY91_180",gwef011);
            }else if(cnt>=181 && cnt <=540){
                gwef.put("DAY181_540",gwef011);
            }else if(cnt>540){
                gwef.put("DAY540UP",gwef.getBigDecimal("GWEF011"));
            }

        }
        return gwefs;
    }

    private int longOfTwoDate(Date first,Date second){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(first);
        int cnt = 0;
        while(calendar.getTime().compareTo(second)!=0){
            calendar.add(Calendar.DATE, 1);
            cnt++;
        }
        return cnt;
    }
}
